package com.mijie.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

public class StringFilter {

	private Map<Character, List<String>> wordMap = null;
	private Logger logger = Logger.getLogger(StringFilter.class);
	private static StringFilter instance = null;
	private File file = null;
	
	private StringFilter(File file){
		this.file  = file;
		wordMap = new ConcurrentHashMap<Character, List<String>>();
		loadWordMap();
		logger.info("load word from file->"+file.getAbsolutePath());
	}
	
	public static StringFilter getInstanct(){
		return instance;
	}
	
	protected static void init(File file){
		if(file.exists() && file.isFile()){
			instance = new StringFilter(file);
		}
	}
	
	public void reload(){
		if(wordMap!=null) wordMap.clear();
		loadWordMap();
		logger.info("reload word from file->"+file.getAbsolutePath());
	}
	
	private void loadWordMap() {
		BufferedReader br = null;
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream(file),"UTF-8");
			br  = new BufferedReader(isr);
			String temp = null;
			while ((temp = br.readLine()) != null) {
				List<String> list = null;
				if (!wordMap.containsKey(temp.charAt(0))) {
					list = new ArrayList<String>();
					wordMap.put(temp.charAt(0), list);
				} else {
					list = wordMap.get(temp.charAt(0));
				}
				list.add(temp);
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public boolean containsDisabledWord(String src){
		String[] model = null;
		String find = null;
		String tmp = null;
		Character c ;
		try {
			for (int i = 0; i < src.length(); i++) {
				c = src.charAt(i);
				if (wordMap.containsKey(c)) {
					List<String> list = wordMap.get(c);
					for (String str : list) {
						// 每个敏感词={自杀，***}
						model = str.split(",");
						// 从该字符开始，截取和此敏感词长度相同的字符
						tmp = src.substring(i, (model[0].length() <= src.length()- i) ? i + model[0].length() : i);

						if (model[0].equals(tmp)) return true;
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return false;
	}
	
	public static void main(String[] args){
		String str = "50后，基本穷逼，晃悠着；60后，政治年代，多数当官了；70后，赶上改革开放吹满地，发了；80后，多数50后穷逼的子女，苦逼了；90后和00后，60后和70后的子女，除了富二代就是官二代。环环相扣的，这下明白了吧？苦逼80后的出现不是无缘无故的。——王朔";
		StringFilter filter = new StringFilter(new File("/Users/zhengzb/dev/workspace/homiServer/resource/disabled_words.txt"));
		System.out.println(filter.filter(str));
	}
	
	public String filter(String src){
		StringBuffer strb = new StringBuffer();
		String[] model = null;
		String find = null;
		String tmp = null;
		Character c ;
		try {
			for (int i = 0; i < src.length(); i++) {
				c = src.charAt(i);
				if (wordMap.containsKey(c)) {
					List<String> list = wordMap.get(c);
					for (String str : list) {
						// 每个敏感词={自杀，***}
						model = str.split(",");
						// 从该字符开始，截取和此敏感词长度相同的字符
						tmp = src.substring(i, (model[0].length() <= src.length()- i) ? i + model[0].length() : i);
						if (model[0].equals(tmp)) {
							find = model[0];
							break;
						}else{
						}
					}
				}

				if (find != null) {
					strb.append(model[1]);
					i += find.length() - 1;
				} else {
					strb.append(c);
				}
				find = null;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
		return strb.toString();
	}

	public File getFile() {
		return file;
	}
	
}
